//
//  VisitorView.swift
//  weibo92
//
//  Created by Apple on 15/10/17.
//  Copyright © 2015年 miao. All rights reserved.
//

import UIKit

//访客视图监听事件协议
protocol VisitorViewDelegate:NSObjectProtocol {
    //注册
    func visitorViewDidRegister()
    //登录
    func visitorViewDidLogin()
    
}
class VisitorView: UIView {
    
    //设置代理
    weak var delegate: VisitorViewDelegate?
    
    //MARK: -监听方法
    @objc func clickRegister() {
        delegate?.visitorViewDidRegister()
    }
    @objc func clickLogin() {
        delegate?.visitorViewDidLogin()
    }

    //MARK: -设置视图信息
    func setupInfo(iconName: String?, title: String){
        messageLabel.text = title
        
        //如果为空 ，首页
        guard let iconName = iconName else{
            startAim()
            return
        }
        // 不是首页, 重新设置图片
        iconView.image = UIImage(named: iconName)
        //隐藏小房子
        homeView.hidden = true
        
        //把遮盖图片移到后面
        sendSubviewToBack(maskIcon)
   
    }
    

    //MARK: -开启动画
    func startAim() {
        
        let aim = CABasicAnimation(keyPath: "transform.rotation")
        ///转的角度
        aim.toValue = 2 * M_PI
        ///转的次数
        aim.repeatCount = MAXFLOAT
        
        ///每次转转的时间
        aim.duration = 10
        
        aim.removedOnCompletion = false
        
        iconView.layer.addAnimation(aim, forKey: "transform.rotation")
        
    }
    //纯代码调用
    override init(frame: CGRect) {
        super.init(frame: frame)

        setUI()
    }
    //sb开发的入口
    //使用sb开发的时候 ，调用这个视图，程序会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK： －懒加载添加图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    // 添加小房子
    private lazy var homeView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //添加文字
    private lazy var messageLabel: UILabel = {
        let message = UILabel()
        message.text = "关注一些人，回这里看看有什么惊喜关注一些人"
        message.numberOfLines = 0
        message.textAlignment = NSTextAlignment.Center
        
        message.textColor = UIColor.darkGrayColor()
        message.font = UIFont.systemFontOfSize(14)
        
        return message
    }()
    
    //添加注册按钮
    private lazy var registerButton: UIButton = UIButton(titleName: "注册", titleColor: UIColor.orangeColor(), backgroundImageName: "common_button_white_disable")

    //添加登陆按钮
    private lazy var loginButton: UIButton = UIButton(titleName:"登录", titleColor: UIColor.darkGrayColor(), backgroundImageName: "common_button_white_disable")

    private lazy var maskIcon: UIImageView = {
        
        let mask = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
        
        return mask
    }()

}



extension VisitorView {

    ///MARK: -  设置访客视图
    private func setUI()
    {
        //1.添加控件
        addSubview(iconView)
        addSubview(maskIcon)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.设置自动布局

        //添加约束
        //默认true  设置控件支持setframe布局  false 支持 自动布局
//        iconView.translatesAutoresizingMaskIntoConstraints = false
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //父控件添加约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: homeView, attribute: .Bottom, multiplier: 1.0, constant: 60))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 70))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant:-70))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //遮盖图片
//        addConstraint(NSLayoutConstraint(item: maskIcon, attribute: .CenterX, relatedBy: .Equal, toItem: homeView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: maskIcon, attribute: .Bottom, relatedBy: .Equal, toItem: iconView , attribute: .Bottom, multiplier: 1.0, constant: 0))
        
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views:["mask":maskIcon]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[loginbtn]", options: [], metrics: ["btnHeight": -34], views: ["mask":maskIcon,"loginbtn":loginButton]))
  
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        ///  添加按钮监听事件
        registerButton.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        loginButton.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)

    }

}
